package com.nsoft.thickclient.trackMove.party;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.util.Date;

import javax.swing.Box;
import javax.swing.JPanel;

import com.nsoft.server.service.main.PartyService;
import com.nsoft.server.serviceImpl.FacadeFactoryImpl;
import com.nsoft.thickclient.beanUtils.ActionDescriptor;
import com.nsoft.thickclient.beanUtils.NButton;
import com.nsoft.thickclient.controller.ScreenController;
import com.nsoft.thickclient.model.ModelEvent;
import com.nsoft.thickclient.model.ModelListener;
import com.nsoft.thickclient.trackMove.intr.party.Party;

public class PartyViewController extends ScreenController implements
		ModelListener {

	private JPanel mainPanel;

	private PartyView partyView;

	private ActionDescriptor saveAction = new ActionDescriptor("Save",
			"doSave", this);

	private ActionDescriptor newAction = new ActionDescriptor("New", "doNew",
			this);

	private ActionDescriptor cancelAction = new ActionDescriptor("Cancel",
			"doCancel", this);

	private NButton saveButton;

	private boolean editMode;

	public PartyViewController() {
		editMode = false;
	}

	public PartyViewController(boolean editinMode) {
		this.editMode = editinMode;
	}

	@Override
	public void close() {
		partyView = null;
		mainPanel = null;
	}

	@Override
	public String getScreenTitle() {
		return "Manage party";
	}

	@Override
	public JPanel getView() {
		return mainPanel;
	}

	public void init() {
		mainPanel = new JPanel(new BorderLayout());
		partyView = new PartyView(null);
		mainPanel.add(partyView, BorderLayout.PAGE_START);
		mainPanel.add(getButtonPanel(), BorderLayout.PAGE_END);
		partyView.getModel().addModelListener(this);

	}

	private JPanel getButtonPanel() {
		JPanel butonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
		saveButton = new NButton(saveAction, editMode);
		butonPanel.add(saveButton);
		butonPanel.add(Box.createHorizontalStrut(20));
		butonPanel.add(new NButton(newAction, !editMode));
		butonPanel.add(Box.createHorizontalStrut(20));
		butonPanel.add(new NButton(cancelAction));
		return butonPanel;
	}

	public void doSave() {
		PartyService service = FacadeFactoryImpl.getPartyService();
		Party party =service.saveParty((Party)partyView.getModel().getValueObject());
		if (!editMode) {
			doNew();
		} else {
			doCancel();

		}
	}

	public void doNew() {
		partyView.getModel().setValueObject(new Party());
		partyView.getModel().setValue("crteationDate", new Date());
	}

	public void doCancel() {
		getApplicationController().closeScreen(this);
	}

	public PartyView getPartyView() {
		return partyView;
	}

	public void setPartyView(PartyView partyView) {
		this.partyView = partyView;
	}

	public boolean isEditinMode() {
		return editMode;
	}

	public void setEditinMode(boolean editinMode) {
		this.editMode = editinMode;
	}

	public void propertyChanged(ModelEvent event, Object oldValue) {
		saveButton.setEnabled(event.getModel().isValied());

	}

	public void valueObjectChanged(ModelEvent event) {
		saveButton.setEnabled(event.getModel().isValied());
	}
}
